let http = require('http');
let querystring =require('querystring');
module.exports = function (url,rags) {
  
  return new Promise((resvole,reject)=>{

      const postData = querystring.stringify(rags);

      var arr=url.split('/');
      var hh=arr[2].split(":");
      var ap="/"+arr.slice(3).join("/");

      const options = {
        hostname: hh[0],
        port: hh[1]?hh[1]:80,
        path: ap,
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': Buffer.byteLength(postData)
        }
      };

      const req = http.request(options, (res) => {
        console.log(`状态码: ${res.statusCode}`);
        console.log(`响应头: ${JSON.stringify(res.headers)}`);
       
        var str='';

        res.setEncoding('utf8');
        res.on('data', (chunk) => {
          str+=chunk;
        });
        res.on('end', () => {
          resvole(str);
        });
      });

      req.on('error', (e) => {
        console.error(`请求遇到问题: ${e.message}`);
      });

      // 写入数据到请求主体
      req.write(postData);
      req.end();
  });
}
